package cn.icanci.ladybird.controller;

import cn.icanci.ladybird.common.result.JsonData;
import cn.icanci.ladybird.common.util.cache.RedisUtils;
import cn.icanci.ladybird.pojo.bo.UserClassPagesBO;
import cn.icanci.ladybird.pojo.dto.UserClassDetailsDto;
import cn.icanci.ladybird.service.TicketDetailsService;
import cn.icanci.ladybird.service.user.UserClassService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 本类用于:
 *
 * @author: wirdeo
 * @date: Created in 2020/10/2 20:42
 * @classAction: 分类页面
 */
@SuppressWarnings("all")
@Controller
@RequestMapping("/api/v1/pub/user")
public class UserClassController {
    @Autowired
    private UserClassService userClassService;
    @Autowired
    private TicketDetailsService ticketDetailsService;
    @Autowired
    private RedisUtils redisUtils;

    @ResponseBody
    @RequestMapping("/class")
    public JsonData getUserClass() {
        List<UserClassDetailsDto> userClassDetailsDtoPageInfo = userClassService.userClassDetailsDtoList();
        return JsonData.builderSuccess(userClassDetailsDtoPageInfo);
    }

    @Deprecated
    @RequestMapping("/json/class2")
    public String getUserClassJson2(Model model, Integer pageNum, Integer pageSize) {
        //查询列表
        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo = userClassService.queryTicketDetailsByCenter(pageNum, pageSize);

        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo1 = userClassService.queryTicketDetailsByCenterUp(pageNum, pageSize);

        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo2 = userClassService.queryTicketDetailsByCenterCreatTime(pageNum, pageSize);

        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo3 = userClassService.queryTicketDetailsByCenterCreatTimeDown(pageNum, pageSize);
        ArrayList<Object> list = new ArrayList<>();
        list.add(userClassDetailsDtoPageInfo);
        list.add(userClassDetailsDtoPageInfo1);
        list.add(userClassDetailsDtoPageInfo2);
        list.add(userClassDetailsDtoPageInfo3);
        JsonData jsonData = JsonData.builderSuccess(list);
        model.addAttribute("jsonData", jsonData);
        return "user/class";
    }

    @Deprecated
    @RequestMapping("/json/class3")
    public String getUserClassJson3(Model model, Integer pageNum1, Integer pageSize1, Integer pageNum2, Integer pageSize2, Integer pageNum3, Integer pageSize3, Integer pageNum4, Integer pageSize4, Integer index) {
        //查询列表
        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo = userClassService.queryTicketDetailsByCenter(pageNum1, pageSize1);
        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo1 = userClassService.queryTicketDetailsByCenterUp(pageNum2, pageSize2);
        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo2 = userClassService.queryTicketDetailsByCenterCreatTime(pageNum3, pageSize3);
        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo3 = userClassService.queryTicketDetailsByCenterCreatTimeDown(pageNum4, pageSize4);
        ArrayList<Object> list = new ArrayList<>();
        list.add(userClassDetailsDtoPageInfo);
        list.add(userClassDetailsDtoPageInfo1);
        list.add(userClassDetailsDtoPageInfo2);
        list.add(userClassDetailsDtoPageInfo3);
        list.add(index);
        System.out.println(list);
        JsonData jsonData = JsonData.builderSuccess(list);
        model.addAttribute("jsonData", jsonData);
        return "user/class";
    }

    @RequestMapping("/json/class")
    public String getUserClassJson(Model model, UserClassPagesBO userClassPagesBO) {
        //查询列表
        // TODO 页面分页有Bug
        // 从Redis中获取数据
//        JsonData targetData = (JsonData) redisUtils.get(RedisCacheKeyManager.CLASS_KEY);
//        if (null == targetData) {
        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo = userClassService.queryTicketDetailsByCenter(userClassPagesBO.getPageNum1(), userClassPagesBO.getPageSize1());
        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo1 = userClassService.queryTicketDetailsByCenterUp(userClassPagesBO.getPageNum2(), userClassPagesBO.getPageSize2());
        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo2 = userClassService.queryTicketDetailsByCenterCreatTime(userClassPagesBO.getPageNum3(), userClassPagesBO.getPageSize3());
        PageInfo<UserClassDetailsDto> userClassDetailsDtoPageInfo3 = userClassService.queryTicketDetailsByCenterCreatTimeDown(userClassPagesBO.getPageNum4(), userClassPagesBO.getPageSize4());
        ArrayList<Object> list = new ArrayList<>();
        list.add(userClassDetailsDtoPageInfo);
        list.add(userClassDetailsDtoPageInfo1);
        list.add(userClassDetailsDtoPageInfo2);
        list.add(userClassDetailsDtoPageInfo3);
        list.add(userClassPagesBO.getIndex());
        JsonData jsonData = JsonData.builderSuccess(list);
        model.addAttribute("jsonData", jsonData);
//            redisUtils.set(RedisCacheKeyManager.CLASS_KEY, jsonData, 30, TimeUnit.MINUTES);
//        } else {
//            model.addAttribute("jsonData", targetData);
//        }
        return "user/class";
    }

}
